﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media.Media3D;

namespace GameBoard
{
    [DataContract]
    public class TerrainViewModel
    {

        [DataMember]
        private List<BaseVisualFactory> _terrainVisualFactories = new List<BaseVisualFactory>();
        public List<BaseVisualFactory> TerrainVisualFactories
        {
            get { return _terrainVisualFactories; }
            set { _terrainVisualFactories = value; }
        }



        [DataMember]
        private ObservableCollection<BaseTerrainData> _placedVisualsList
            = new ObservableCollection<BaseTerrainData>();
        public ObservableCollection<BaseTerrainData> PlacedVisualsList
        {
            get
            {
                return _placedVisualsList;
            }
            set
            {
                _placedVisualsList = value;
            }
        }
        
        private void RegisterPlacedVisual(BaseTerrainData newData)
        {
            PlacedVisualsList.Add(newData);
        }
        public enum ShapeMode
        {
            None, Sphere, Cone, Line, Square,
            ObjectFile
        }
        private ShapeMode _shapeSelection;
        public ShapeMode ShapePlacementType
        {
            get { return _shapeSelection; }
            set
            {
                _shapeSelection = value;
                VisualsViewModel.Instance.SetMouseCursorToAoEFor(value);
            }
        }


        private bool _shapeIsPermanent;
        public bool ShapeIsPermanent
        {
            get { return _shapeIsPermanent; }
            set
            {
                _shapeIsPermanent = value;
                OnPropertyChanged("ShapeIsPermanent");
            }
        }


        private bool _shapeIsTransparent;
        public bool ShapeIsTransparent
        {
            get { return _shapeIsTransparent; }
            set
            {
                _shapeIsTransparent = value;
                OnPropertyChanged("ShapeIsTransparent");
            }
        }

        private string _objectPlacementImage;
        public string ObjectPlacementImage
        {
            get 
            {
                return _objectPlacementImage;
            }
            set
            {
                _objectPlacementImage = value;
                OnPropertyChanged("ObjectPlacementImage");
            }
        }

        private int _shapeSizeValue = 20;
        public int ShapeSizeValue
        {
            get { return _shapeSizeValue; }
            set
            {
                _shapeSizeValue = value;
                OnPropertyChanged("ShapeSizeValue");
            }
        }

        private int _shapeWidthValue = 20;
        public int ShapeWidthValue
        {
            get { return _shapeWidthValue; }
            set
            {
                _shapeWidthValue = value;
                OnPropertyChanged("ShapeWidthValue");
            }
        }
        private int _shapeHeightValue = 20;
        public int ShapeHeightValue
        {
            get { return _shapeHeightValue; }
            set
            {
                _shapeHeightValue = value;
                OnPropertyChanged("ShapeHeightValue");
            }
        }
        
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion


        private string _currentObjectFile;
        public string CurrentObjectFile
        {
            get
            {
                return _currentObjectFile;
            }
            set
            {
                _currentObjectFile = value;
                OnPropertyChanged("CurrentObjectFile");
            }
        }




        internal void RemoveTerrainVisualData(Guid guid)
        {
            var match = TerrainVisualFactories.First(x => x.FactoryVisualID == guid);
            TerrainVisualFactories.Remove(match);
            var dataMatch = PlacedVisualsList.First(x => x.FactoryID == guid);
            PlacedVisualsList.Remove(dataMatch);
        }

        internal void PopulateBoardWithTerrain(GameBoardVisual gameBoardVisual)
        {
            foreach (var cur in TerrainVisualFactories)
            {
                gameBoardVisual.AddVisual(cur.FactoryVisualID, cur.MakeVisual());
            }
        }

        internal BaseTerrainData GetTerrainDataFor(Guid guid)
        {
            var matching = TerrainVisualFactories.First(x => x.FactoryVisualID == guid).VisualData;
            return matching as BaseTerrainData;
        }



        internal void RegisterFactory(BaseVisualFactory newFactory)
        {
            TerrainVisualFactories.Add(newFactory);
            RegisterPlacedVisual(newFactory.VisualData as BaseTerrainData);
        }

        internal void ClearViewModel()
        {
            PlacedVisualsList.Clear();
            TerrainVisualFactories.Clear();
        }

        internal void PopulateFromViewModel(TerrainViewModel terrainViewModel)
        {
            foreach (var cur in terrainViewModel.TerrainVisualFactories)
            {
                TerrainVisualFactories.Add(cur);
            }
            foreach (var cur in terrainViewModel.PlacedVisualsList)
            {
                PlacedVisualsList.Add(cur);
            }
        }
    }
}
